package view.login;

import library.controller.LoginListener;
import view.CURD.UserDialog;

import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author unknown
 */
public class LoginDialog extends JDialog {
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel NPPanel;
    private JLabel userNameLabel;
    private JLabel passwordLabel;
    private JPanel panel1;
    private JTextField idField;
    private JPasswordField passwordField;
    private JPanel panel2;
    private JRadioButton reader;
    private JRadioButton bookAdmin;
    private JRadioButton sysAdmin;
    private ButtonGroup radioGroup;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    private LoginListener loginListener;
    private UserDialog userDialog;

    public LoginDialog() {
        this.initComponents();
    }

    private void initComponents() {
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        NPPanel = new JPanel();
        userNameLabel = new JLabel();
        passwordLabel = new JLabel();
        panel1 = new JPanel();
        idField = new JTextField();
        passwordField = new JPasswordField();
        panel2 = new JPanel();
        reader = new JRadioButton();
        bookAdmin = new JRadioButton();
        sysAdmin = new JRadioButton();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();
        loginListener = new LoginListener(this,this.idField,this.passwordField);
        radioGroup = new ButtonGroup();
        reader.addActionListener(loginListener);
        bookAdmin.addActionListener(loginListener);
        sysAdmin.addActionListener(loginListener);
        okButton.addActionListener(loginListener);
        cancelButton.addActionListener(loginListener);
        //======== Login ========
        {
            this.setTitle("Login");
            Container LoginContentPane = this.getContentPane();
            LoginContentPane.setLayout(new BorderLayout());

            //======== dialogPane ========
            {
                dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
                dialogPane.setBorder ( new CompoundBorder ( new TitledBorder ( new EmptyBorder ( 0, 0 ,0 , 0) ,  "" , TitledBorder. CENTER , TitledBorder . BOTTOM, new Font ( "Dialog", Font. BOLD ,12 ) , Color .red ) , dialogPane. getBorder () ) );
                dialogPane. addPropertyChangeListener( new PropertyChangeListener ( ){ @Override public void propertyChange (PropertyChangeEvent e) { if( "border" .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } );
                dialogPane.setLayout(new BorderLayout());

                //======== contentPanel ========
                {
                    contentPanel.setLayout(new BorderLayout(20, 30));

                    //======== NPPanel ========
                    {
                        NPPanel.setLayout(new GridLayout(2, 1, 0, 20));

                        //---- userNameLabel ----
                        userNameLabel.setText("用户名：");
                        userNameLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
                        NPPanel.add(userNameLabel);

                        //---- passwordLabel ----
                        passwordLabel.setText("密码：");
                        passwordLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
                        NPPanel.add(passwordLabel);
                    }
                    contentPanel.add(NPPanel, BorderLayout.WEST);

                    //======== panel1 ========
                    {
                        panel1.setLayout(new GridLayout(2, 1, 0, 20));
                        panel1.add(idField);
                        panel1.add(passwordField);
                    }
                    contentPanel.add(panel1, BorderLayout.CENTER);

                    //======== panel2 ========
                    {
                        panel2.setLayout(new GridLayout(1, 3));

                        //---- reader ----
                        reader.setText("读者");
                        reader.setSelected(true);
                        reader.setActionCommand("Reader");
                        radioGroup.add(reader);
                        panel2.add(reader);

                        //---- bookAdmin ----
                        bookAdmin.setText("图书管理员");
                        bookAdmin.setActionCommand("BookAdmin");
                        panel2.add(bookAdmin);
                        radioGroup.add(bookAdmin);

                        //---- sysAdmin ----
                        sysAdmin.setText("系统管理员");
                        sysAdmin.setActionCommand("SysAdmin");
                        panel2.add(sysAdmin);
                        radioGroup.add(sysAdmin);
                    }
                    contentPanel.add(panel2, BorderLayout.NORTH);
                }
                dialogPane.add(contentPanel, BorderLayout.CENTER);

                //======== buttonBar ========
                {
                    buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                    buttonBar.setLayout(new GridBagLayout());
                    ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                    ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                    //---- okButton ----
                    okButton.setText("登录");
                    okButton.setActionCommand("Login");
                    buttonBar.add(okButton, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 0), 0, 0));

                    //---- cancelButton ----
                    cancelButton.setText("注册");
                    cancelButton.setActionCommand("Create");
                    buttonBar.add(cancelButton, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
                }
                dialogPane.add(buttonBar, BorderLayout.SOUTH);
            }
            LoginContentPane.add(dialogPane, BorderLayout.CENTER);
            this.pack();
            this.setLocationRelativeTo(this.getOwner());
            this.setVisible(true);
            this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            this.validate();
        }
    }
    public void setUserDialog(UserDialog userDialog){
        this.userDialog = userDialog;
    }
    public LoginListener getLoginListener(){
        return this.loginListener;
    }
}
